using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class GlobalVariableGetterInstruction:InstructionBase
    {
        #region props
        /// <summary>
        /// Global variable name (SrcFieldName) --> FieldGetter
        /// </summary>
        private List<string> _GlobalVariableNames;

        /// <summary>
        /// 
        /// </summary>
        public List<string> GlobalVariableNames
        {
            get { return _GlobalVariableNames; }
            set { _GlobalVariableNames = value; }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public GlobalVariableGetterInstruction()
            :base(Guid.Empty,Guid.Empty)
        {
            this._GlobalVariableNames = null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="varNames"></param>
        public GlobalVariableGetterInstruction(
            Guid componentID, Guid jobTicketID, List<string> varNames) 
            : base(componentID, jobTicketID)
        {
            this._GlobalVariableNames = varNames;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            if (this.GlobalVariableNames != null && this.GlobalVariableNames.Count > 0)
            {
                XmlNode varRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "Getters");
                foreach (string varName in this.GlobalVariableNames)
                {
                    XmlNode getterNode = XmlDataUtil.AddElement(ref xDoc, varRootNode, "Getter");
                    XmlDataUtil.UpdateAttribute(ref xDoc,getterNode,"GlobalVariableName",varName);
                }
            }

            return xNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                GlobalVariableGetterInstruction instruction = new GlobalVariableGetterInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.GlobalVariableNames = null;
                XmlNodeList getterNodes = xNode.SelectNodes("Getters/Getter");
                if (getterNodes != null && getterNodes.Count > 0)
                {
                    instruction.GlobalVariableNames=new List<string>();
                    foreach (XmlNode getterNode in getterNodes)
                    {
                        instruction.GlobalVariableNames.Add(getterNode.Attributes.GetNamedItem("GlobalVariableName").Value);
                    }
                }

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }
    }
}
